<template>
  <div id="LessonPlan">
    <a-form layout="inline" style="margin-bottom: 8px">
      <a-form-item label="关键字" class="searchItem">
        <a-input v-model="coursePlanName" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="">
        <a-button type="primary" @click="getData" style="margin-left: 10px">
          搜索
        </a-button>
        <a-button type="primary" @click="addClick" style="margin-left: 10px">
          新增
        </a-button>
      </a-form-item>
    </a-form>
    <a-table
      bordered
      :scroll="{ y: 500 }"
      :columns="columns"
      style="margin-top: 16px"
      :data-source="tableData"
      :pagination="tablePagination"
      :rowKey="(record, index) => index"
    >
      <template slot="name" slot-scope="text, record">
        <a-tooltip placement="top" v-if="record.name">
          <template slot="title">
            <span>{{ record.name }}</span>
          </template>
          <span style="cursor: default">{{ record.name }}</span>
        </a-tooltip>
        <span v-else-if="!record.name">—</span>
      </template>
      <template slot="tapdProjectId" slot-scope="text, record">
        <a-tooltip placement="top" v-if="record.tapdProjectId">
          <template slot="title">
            <p>{{ record.tapdProjectId }}</p>
          </template>
          <span style="cursor: default">{{ record.tapdProjectId }}</span>
        </a-tooltip>
        <span v-else-if="!record.tapdProjectId">—</span>
      </template>
      <template slot="operation" slot-scope="text, record">
        <a-button v-if="role == 3" type="primary" @click="editClick(record)"
          >修改信息</a-button
        >
        <a-button
          type="primary"
          @click="viewClick(record)"
          style="margin-left: 10px"
          >课程资源</a-button
        >
        <a-button
          v-if="role == 3"
          type="danger"
          style="margin-left: 10px"
          @click="deleteClick(record)"
          >删除</a-button
        >
      </template>
    </a-table>
    <a-modal v-model="visible" :title="title" @cancel="handleCancel">
      <a-form-model
        :label-col="{ span: 8 }"
        :wrapper-col="{ span: 12 }"
        ref="formRef"
        :model="form"
        :rules="rules"
      >
        <a-form-model-item label="课程名称" prop="courseName">
          <a-select v-model="form.courseName">
            <a-select-option
              v-for="item in classNameList"
              :key="item.id"
              :value="item.id"
              >{{ item.name }}</a-select-option
            >
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="开课时间" prop="time">
          <a-date-picker v-model="form.time" style="width: 236px" />
        </a-form-model-item>
        <a-form-model-item label="开课地点" prop="location">
          <a-input v-model="form.location" />
        </a-form-model-item>
        <a-form-model-item label="开课老师" prop="teacher">
          <a-input v-model="form.teacher" />
        </a-form-model-item>
        <a-form-model-item label="参与学员所属届" prop="year">
          <a-select v-model="form.year">
            <a-select-option
              v-for="item in yearList"
              :key="item"
              :value="item"
              >{{ item }}</a-select-option
            >
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="参与学员所属班级" prop="studentClass">
          <a-select v-model="form.studentClass">
            <a-select-option
              v-for="item in classList"
              :key="item"
              :value="item"
              >{{ item }}</a-select-option
            >
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="训练计划" prop="trainingPlan">
          <a-select
            v-model="form.trainingPlan"
            mode="multiple"
            :maxTagCount="1"
          >
            <a-select-option
              v-for="item in trainingPlanList"
              :key="item.id"
              :value="item.id"
              >{{ item.name }}</a-select-option
            >
          </a-select>
        </a-form-model-item>
      </a-form-model>
      <template slot="footer">
        <a-button key="back" class="modalCancel" @click="handleCancel"
          >取消</a-button
        >
        <a-button key="submit" @click="primaryClick" type="primary">
          确定
        </a-button>
      </template>
    </a-modal>
  </div>
</template>
  
<script>
import moment from "moment";
export default {
  props: {
    role: {}, //3是管理,1是学员
  },
  data() {
    return {
      coursePlanName: "",
      classList: [],
      yearList: [],
      classNameList: [],
      trainingPlanList: [],
      form: {
        id: null, //课程计划id
        courseName: "", // 课程计划名称
        time: "", //开课时间
        location: "", //开课地点
        teacher: "", //开课老师
        year: "", //参与学员所属届
        studentClass: "", //参与学员所属班级
        trainingPlan: [], //训练计划
      },
      rules: {
        courseName: [
          { required: true, message: "请选择课程名称", trigger: "blur" },
        ],
        time: [{ required: true, message: "请输入开课时间", trigger: "blur" }],
        location: [
          { required: true, message: "请输入开课地点", trigger: "blur" },
        ],
        teacher: [
          { required: true, message: "请输入开课老师", trigger: "blur" },
        ],
        year: [
          { required: true, message: "请选择参与学员所属届", trigger: "blur" },
        ],
        studentClass: [
          {
            required: true,
            message: "请选择参与学员所属班级",
            trigger: "blur",
          },
        ],
        trainingPlan: [
          {
            required: true,
            message: "请选择训练计划",
            trigger: "blur",
          },
        ],
      },
      visible: false,
      title: null,
      columns: [
        //主页table列的配置
        {
          title: "序列号",
          dataIndex: "key",
          scopedSlots: { customRender: "key" },
        },
        {
          title: "课程计划名称",
          dataIndex: "courseName",
          customRender: (text, record) => {
            return record.courseName ? record.courseName : "—";
          },
        },
        {
          title: "开课时间",
          dataIndex: "time",
          customRender: (text, record) => {
            return record.time ? record.time : "—";
          },
        },
        {
          title: "开课地点",
          dataIndex: "location",
          customRender: (text, record) => {
            return record.location ? record.location : "—";
          },
        },
        {
          title: "开课老师",
          dataIndex: "teacher",
          customRender: (text, record) => {
            return record.teacher ? record.teacher : "—";
          },
        },
        {
          title: "参与学员所属届",
          dataIndex: "year",
          customRender: (text, record) => {
            return record.year ? record.year : "—";
          },
        },
        {
          title: "参与学员所属班级",
          dataIndex: "studentClass",
          customRender: (text, record) => {
            return record.studentClass ? record.studentClass : "—";
          },
        },
        {
          title: "训练计划",
          dataIndex: "trainingPlan",
          customRender: (text, record) => {
            return record.trainingPlan ? record.trainingPlan : "—";
          },
        },
        {
          title: "操作",
          width: 300,
          dataIndex: "operation",
          scopedSlots: { customRender: "operation" },
        },
      ],
      tableData: [
        //主页表格的数据
      ],
      tablePagination: {
        //主页表格的分页器
        total: 0,
        current: 1,
        size: "10",
        showQuickJumper: true, //是否可以快速跳转至某页
        showSizeChanger: true, //是否可以改变 pageSize
      },
    };
  },
  created() {
    this.getData();
    this.getClassNameList(); //获取课程名称的下拉
    this.getClassList(); //获取所属班级的下拉
    this.getYearList(); //获取所属届的下拉
    this.getTrainingPlanList(); //获取训练计划
  },
  methods: {
    deleteClick(row) {
      //点击删除
      let params = {
        ids: row.id,
      };
      let that = this;
      this.$confirm({
        title: "确认删除?",
        content: "您确定要删除此项吗?",
        onOk() {
          that.$axios.delete("/sys/coursePlan", { params }).then((res) => {
            that.$message.success("删除成功");
            that.getData();
          });
        },
        onCancel() {
          console.log("取消删除");
        },
      });
    },
    getTrainingPlanList() {
      //训练计划的下拉
      this.$axios.get("/sys/subject/page").then((res) => {
        this.trainingPlanList = res.response;
      });
    },
    getClassNameList() {
      //获取课程名称的下拉
      this.$axios
        .get("/sys/course/page", {
          params: {
            courseName: "",
          },
        })
        .then((res) => {
          this.classNameList = res.response.map((item) => ({
            id: item.id,
            name: item.name,
          }));
        });
    },
    getClassList() {
      //获取所属班级的下拉
      this.$axios.get("/sys/user/studentClass").then((res) => {
        this.classList = res.response;
      });
    },
    getYearList() {
      //获取所属届的下拉
      this.$axios.get("/sys/user/year").then((res) => {
        this.yearList = res.response;
      });
    },
    addClick() {
      this.title = "新增";
      this.visible = true;
    },
    editClick(row) {
      //编辑
      this.$axios
        .get("/sys/coursePlan", {
          params: {
            id: row.id,
          },
        })
        .then((res) => {
          this.form.id = row.id;
          this.form.courseName = res.response.courseId;
          this.form.time = res.response.time ? moment(res.response.time) : null;
          this.form.location=res.response.location;
          this.form.teacher=res.response.teacher;
          this.form.year=res.response.year;
          this.form.studentClass=res.response.studentClass;
          this.form.trainingPlan=res.response.subjectIds?res.response.subjectIds.split(","):[];
          this.title = "编辑";
          this.visible = true;
        });
    },
    handleCancel() {
      this.form= {
        id: null, //课程计划id
        courseName: "", // 课程计划名称
        time: "", //开课时间
        location: "", //开课地点
        teacher: "", //开课老师
        year: "", //参与学员所属届
        studentClass: "", //参与学员所属班级
        trainingPlan: [], //训练计划
      };
      this.visible = false;
    },
    primaryClick() {
      this.$nextTick(() => {
        this.$refs.formRef.validate((valid) => {
          if (valid) {
            let data = {
              id: this.form.id,
              courseId: this.form.courseName, // 课程名称
              time: this.form.time
                ? moment(this.form.time).format("YYYY-MM-DD")
                : null, //开课时间
              location: this.form.location, //开课地点
              teacher: this.form.teacher, //开课老师
              year: this.form.year, //参与学员所属届
              studentClass: this.form.studentClass, //参与学员所属班级
              subjectIds:
                this.form.trainingPlan.length > 0
                  ? this.form.trainingPlan.join(",")
                  : null, //训练计划
            };
            if (this.title == "新增") {
              this.$axios.post("/sys/coursePlan", data).then((res) => {
                this.$message.success("新增成功");
                this.getData();
                this.handleCancel();
              });
            } else if (this.title == "编辑") {
              this.$axios.put("/sys/coursePlan", data).then((res) => {
                this.$message.success("编辑成功");
                this.getData();
                this.handleCancel();
              });
            }
          } else {
            console.log("校验失败");
          }
        });
      });
    },
    async getData() {
      let data = await this.$axios.get("/sys/coursePlan/page", {
        params: {
          coursePlanName: this.coursePlanName,
          page: this.tablePagination.current,
          limit: this.tablePagination.size,
        },
      });

      data.response.forEach((item, index) => {
        item.key = index + 1;
      });
      this.tableData = data.response;
      this.tablePagination.total = data.response.length;
    },
    viewClick(row) {
      this.$emit("changeIsDetail", {
        data: row,
        status: true,
      });
    },
  },
};
</script>
  
  <style>
</style>